package com.nwpu.coursesupport.view.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.controller.AbstractController;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.SubmitTaskStatus;
import com.nwpu.coursesupport.enumeration.TaskType;
import com.nwpu.coursesupport.service.imple.TaskListStuViewService;
import com.nwpu.coursesupport.view.criteria.TaskListStuViewSearchCriteria;

@Controller
@RequestMapping("/taskListStuView")
public class TaskListStuViewController extends AbstractController{

	@Autowired
	private TaskListStuViewService taskListStuViewService;
	
	@RequestMapping("/taskList")
	public String taskList(@RequestParam("classId")int classId,Integer pageNo,String type,HttpServletRequest request){
		TaskListStuViewSearchCriteria sc=new TaskListStuViewSearchCriteria();
		int id=(int) getSession().getAttribute("id");
		
		sc.setStudentId(id);
		sc.setClassId(classId);
		sc.setPageNo(pageNo==null?1:pageNo);
		if(type==null){
			
		}else if(type.equals(TaskType.ASKINLESSON.name())){
			sc.setType(TaskType.ASKINLESSON);
		}else if(type.equals(TaskType.HOMEWORK.name())){
			sc.setType(TaskType.HOMEWORK);
		}else if(type.equals(TaskType.LABTEST.name())){
			sc.setType(TaskType.LABTEST);
		}else if(type.equals(TaskType.TASKMAIN.name())){
			sc.setType(TaskType.TASKMAIN);
		}else if(type.equals(TaskType.TESTINLESSON.name())){
			sc.setType(TaskType.TESTINLESSON);
		};
		JSONResult rst=this.taskListStuViewService.taskList(sc);
		request.setAttribute("data", this.parseData(rst));
		return "/stupage/classTask";
	}
	
	@RequestMapping("/ajaxTaskList")
	@ResponseBody
	public JSONResult ajaxTaskList(@RequestParam("data")String data){
		JSONResult rst=invokeService("taskListStuView", "taskList", data);
		return rst;
	}
	
	@RequestMapping("/taskPersonal")
	public String taskPersonalList(@RequestParam("classId")int classId,Integer pageNo,String taskStatus,HttpServletRequest request){
		/*pageNo=pageNo==null?1:pageNo;
		String data="{\"classId\":\"" + classId + "\",\"pageNo\":\""+pageNo+"\"}";
		JSONResult rst=invokeService("taskListStuView", "taskPersonalList",data);
		request.setAttribute("data", this.parseData(rst));*/
		TaskListStuViewSearchCriteria sc=new TaskListStuViewSearchCriteria();
		sc.setClassId(classId);
		sc.setPageNo(pageNo==null?1:pageNo);
		if(taskStatus==null){
			
		}else if(taskStatus.equals(SubmitTaskStatus.UNCOMMITTED.name())){
			sc.setTaskStatus(SubmitTaskStatus.UNCOMMITTED);
		}else if(taskStatus.equals(SubmitTaskStatus.COMMITTED.name())){
			sc.setTaskStatus(SubmitTaskStatus.COMMITTED);
		}else if(taskStatus.equals(SubmitTaskStatus.HAVEREAD.name())){
			sc.setTaskStatus(SubmitTaskStatus.HAVEREAD);
		}
		JSONResult rst=this.taskListStuViewService.taskPersonalList(sc);
		request.setAttribute("data", this.parseData(rst));
		return "/stupage/classTask";
	}
	
	@RequestMapping("/ajaxTaskPersonal")
	@ResponseBody
	public JSONResult ajaxTaskPersonalList(@RequestParam("data")String data){
		JSONResult rst=invokeService("taskListStuView", "taskPersonalList", data);
		return rst;
	}
}
